package com.ryskoo.catering.vendortemplatereldetail.repository;

import com.ryskoo.catering.vendortemplatereldetail.domain.VendorTemplateRelDetailPO;
import com.ryskoo.framework.core.repository.BasicJPAImpl;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class VendorTemplateRelDetailJPAImpl extends BasicJPAImpl<VendorTemplateRelDetailPO> implements IVendorTemplateRelDetailRepository {
    @Override
    public void deleteByMainUuids(List<String> mainUuids) {
        String hql = "delete from VendorTemplateRelDetailPO o where o.relUuid in (:mainUuids)";
        Query query = getH4Session().createQuery(hql);
        query.setParameterList("mainUuids",mainUuids);
        query.executeUpdate();
    }

    @Override
    public List<VendorTemplateRelDetailPO> getByMainUuid(String mainUuid,boolean useStatus,boolean status) {
        String hql = "from VendorTemplateRelDetailPO o where o.relUuid = :mainUuid ";
        if(useStatus){
            hql += " and o.status = :status";
        }
        hql += " order by o.position asc ";
        Query query = getH4Session().createQuery(hql);
        query.setString("mainUuid",mainUuid);
        if(useStatus){
            query.setBoolean("status",status);
        }
        return query.list();
    }

    @Override
    public void changeStatus(String detailUuid, boolean status) {
        String hql = "update VendorTemplateRelDetailPO o set o.status = :status where o.uuid = :uuid";
        Query query = getH4Session().createQuery(hql);
        query.setString("uuid",detailUuid);
        query.setBoolean("status",status);
        query.executeUpdate();
    }
}
